package com.ruoyi.financial.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.common.core.domain.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.financial.mapper.FThingTypeMapper;
import com.ruoyi.financial.domain.FThingType;
import com.ruoyi.financial.service.IFThingTypeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 收支项目Service业务层处理
 * 
 * @author xunzhe
 * @date 2024-08-07
 */
@Service
public class FThingTypeServiceImpl implements IFThingTypeService 
{
    @Autowired
    private FThingTypeMapper fThingTypeMapper;

    /**
     * 查询收支项目
     * 
     * @param typeId 收支项目主键
     * @return 收支项目
     */
    @Override
    public FThingType selectFThingTypeByTypeId(Long typeId)
    {
        return fThingTypeMapper.selectFThingTypeByTypeId(typeId);
    }

    /**
     * 查询收支项目列表
     * 
     * @param fThingType 收支项目
     * @return 收支项目
     */
    @Override
    public List<FThingType> selectFThingTypeList(FThingType fThingType)
    {
        return fThingTypeMapper.selectFThingTypeList(fThingType);
    }

    /**
     * 新增收支项目
     * 
     * @param fThingType 收支项目
     * @return 结果
     */
    @Override
    public int insertFThingType(FThingType fThingType)
    {
        if(fThingType.getParentId() != null){
            FThingType info = fThingTypeMapper.selectFThingTypeByTypeId(fThingType.getParentId());
            if(info != null){
                fThingType.setAncestors(info.getAncestors() + "," + fThingType.getParentId());
            }
        }

        return fThingTypeMapper.insertFThingType(fThingType);
    }

    /**
     * 修改收支项目
     * 
     * @param fThingType 收支项目
     * @return 结果
     */
    @Override
    public int updateFThingType(FThingType fThingType)
    {
        if(fThingType.getParentId() != null){
            FThingType info = fThingTypeMapper.selectFThingTypeByTypeId(fThingType.getParentId());
            if(info != null){
                fThingType.setAncestors(info.getAncestors() + "," + fThingType.getParentId());
            }
        }
        return fThingTypeMapper.updateFThingType(fThingType);
    }

    /**
     * 批量删除收支项目
     * 
     * @param typeIds 需要删除的收支项目主键
     * @return 结果
     */
    @Override
    public int deleteFThingTypeByTypeIds(String typeIds)
    {
        return fThingTypeMapper.deleteFThingTypeByTypeIds(Convert.toStrArray(typeIds));
    }

    /**
     * 删除收支项目信息
     * 
     * @param typeId 收支项目主键
     * @return 结果
     */
    @Override
    public int deleteFThingTypeByTypeId(Long typeId)
    {
        return fThingTypeMapper.deleteFThingTypeByTypeId(typeId);
    }

    /**
     * 查询收支项目树列表
     * 
     * @return 所有收支项目信息
     */
    @Override
    public List<Ztree> selectFThingTypeTree()
    {
        List<FThingType> fThingTypeList = fThingTypeMapper.selectFThingTypeList(new FThingType());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (FThingType fThingType : fThingTypeList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(fThingType.getTypeId());
            ztree.setpId(fThingType.getParentId());
            ztree.setName(fThingType.getTypeName());
            ztree.setTitle(fThingType.getTypeName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    public int checkCount(Long thingId){
        return fThingTypeMapper.checkCount(thingId);
    }

    public List<FThingType> checkName(String thingName){
        return fThingTypeMapper.checkName(thingName);
    }

}
